package ServerStuff;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Business;
import Database.DatabaseConnector;

@WebServlet("/EditBusinessServlet")
public class EditBusinessServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	
	

	  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
			DatabaseConnector dbConnect = new DatabaseConnector();
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			HttpSession session = req.getSession();
			String busName = (String) session.getAttribute("username");
			String password = (String) session.getAttribute("password");
			

			Business profile = dbConnect.getProfile(busName);
		
			JSONObject result = new JSONObject();
			try {
				if (profile != null) {
					result.put("business", profile.getBusname());
					result.put("password", password);
					result.put("telephone", profile.getBusNum());
					result.put("cell", profile.getCell());
					result.put("city", profile.getCity());
					result.put("email", profile.getEmail());
					result.put("fax", profile.getFax());
					result.put("address", profile.getStreetAd());
					result.put("postal", profile.getPostal());
					result.put("province", profile.getProvince());
				}
			} catch (JSONException e) {
			
				e.printStackTrace();
			}
			

			
			res.getWriter().print(result);
			res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
			
		
			
}
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
			DatabaseConnector dbConnect = new DatabaseConnector();
		
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			HttpSession session = req.getSession();
			
			String busName = (String) session.getAttribute("username");
			String newPassword = (String) session.getAttribute("password");
			
			
			if(req.getParameter("password") != "" && req.getParameter("password") != null){
				newPassword = req.getParameter("password");
			}
		
	    	String newEmail = req.getParameter("email");
	    
	    	String newTelNum = req.getParameter("busNum");
	    	String newFax = req.getParameter("fax");
	    	String newCell = req.getParameter("cell");
	    	String newStreetAd = req.getParameter("streetAd");
	    	String newCity = req.getParameter("city");
	    	String newProv = req.getParameter("province");
	    	String newPostalCode = req.getParameter("postalCode");
			
			dbConnect.editBusiness(busName, newPassword, newTelNum, newFax, newCell, newEmail, newStreetAd, newCity, newProv, newPostalCode);
			res.sendRedirect("Business.jsp");
		}
	
	
}
